<template>
  <div>
    <common-head title="提交订单"></common-head>
    <div class="warp">
      <van-contact-card
        type="cardType"
        :name="currentContact.linkMan"
        :tel="currentContact.mobile"
        @click="onEdit"
      />

       <van-swipe-cell v-for="(item) in items" :key="item.id">
            <van-card
              :num="item.number"
              :price="item.price"
              :title="item.name"
              :thumb="item.pic"
            />
        </van-swipe-cell>
         <van-submit-bar
        :price="total * 100"
        button-text="点击支付"
        @submit="onSubmit"
      >
      </van-submit-bar>
    </div>
  </div>
</template>

<script>
import CommonHead from '../../components/CommonHead.vue'
import { readCart, fetchContactDefault, fetchContactDetail } from 'api'
import { getToken } from 'utils'
export default {
  data () {
    return {
      items: [],
      total: 0,
      currentContact: {}
    }
  },
  created () {
    this.readCart()
    this.fetchContact()
  },
  methods: {
    onSubmit () {
      alert('支付页面')
    },
    onEdit () {
      this.$router.push('/contactLists')
    },
    // 获取默认联系人
    // fetchContactDefault () {
    //   fetchContactDefault({
    //     token: getToken()
    //   }).then(res => {
    //     console.log(res)
    //     if (res.data.code === 0) {
    //       this.currentContact = res.data.data.info
    //     }
    //   })
    // },
    // 点击切换联系人，id 传过来了， 但是会初始化页面，还是获取的默认联系人，所以做的判断
    // 如果 有id 就说明，点击切换了，要获取联系人接口， 如果没id 就默认联系人
    fetchContact () {
      if (this.$route.query.id) {
        fetchContactDetail({
          id: this.$route.query.id,
          token: getToken()
        }).then(res => {
          if (res.data.code === 0) {
            this.currentContact = res.data.data.info
          }
        })
      } else {
        fetchContactDefault({
          token: getToken()
        }).then(res => {
          console.log(res)
          if (res.data.code === 0) {
            this.currentContact = res.data.data.info
          }
        })
      }
    },
    // 读取购物车数据
    readCart () {
      readCart({
        token: getToken()
      }).then((res) => {
        // console.log(res)
        if (res.data.code === 0) {
          this.items = res.data.data.items.filter(item => item.selected)
          this.total = res.data.data.price
        }
      })
    }
  },
  computed: {
    cardType () {
      return this.currentContact.id ? 'edit' : 'add'
    }
  },
  components: {
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.warp{
  padding-top: 46px;
  padding-bottom: 50px;
}
</style>
